<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>添加和修改俱乐部</title>
	<%@ include file="/view/common/tags.jsp"%>
</head>
<body>
	<div class="well well_p" >
		<sk:form method="post" cssClass="form-horizontal" action="${sysManagerV1}/pubgClub/savePubgClub" id="saveAndUpdateForm" target="_parent">
    		<input type="hidden" id="rowId" name="rowId" value="${pubgClub.rowId }">
			<div class="form-group">
				<label class="col-xs-3 text-right">俱乐部名称:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="clubName" id="clubName" value="${pubgClub.clubName }" maxlength = "20" placeholder="20个汉字内">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">俱乐部靓号:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="clubNo" id="clubNo" value="${pubgClub.clubNo }" maxlength = "10" placeholder="10个汉字内" onblur="checkClubNoIsExist(this.value)">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">管理员:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="ministerName" id="ministerName" value="${pubgClub.ministerName }" placeholder="a用户,b用户" onblur="valiMinisterNameBlur()">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">介绍:</label>
				<div class="col-xs-7">
					<textarea  class="remark" name="clubIntro" id="clubIntro" placeholder="200汉字以内" maxlength="200">${pubgClub.clubIntro }</textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">邀请码:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="inviteCode" id="inviteCode" value="${pubgClub.inviteCode }" placeholder="" maxlength="6">
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-3 text-right">兑换比例:</label>
				<div class="col-xs-5">
					<input type="text" class="form-control" name="exchageRatio" id="exchageRatio" value="${pubgClub.exchageRatio }" readonly placeholder="1:1">
				</div>
			</div>
			<input type="hidden" id="isReCommit" value="0">
		</sk:form>
	</div>
</body>
   <%@ include file="/view/common/footer.jsp"%>
   <script type="text/javascript">

       //失去焦点事件验证
       //验证填写的管理员
       //查询用户是否存在，是否满足成为部长条件
       function valiMinisterNameBlur() {
           var clubId = $('#rowId').val()
           if (clubId == null) {
               clubId = 0;
           }
           var ministerName = $('#ministerName').val();
           if (CheckUtil.isNotNull(ministerName)) {
               var ministerNameSplit = ministerName.split(",");
               for (i=0;i<ministerNameSplit.length ;i++ ) {
                   if(!CheckUtil.isMobile(ministerNameSplit[i])) {
                       layer.msg('第'+(i+1)+'个帐号格式不正确!');
                       return;
                   }
               }
               $.post(JSSYSMANAGERV1 + "/pubgUser/checkPubgUserInfo?clubId="+clubId+"&userAccountArray="+ministerName,function(result){
                   if (result != null) {
                       if (result.code == '-1') {
                           layer.msg("用户验证出错！");
                           return;
                       } else if (result.code == '-2') {
                           layer.msg(result.message);
                           return;
                       }
                   }
               });
           }
       }
   //添加修改俱乐部
   function add(){
	    var rowId = $('#rowId').val();
	    var clubName = $('#clubName').val();
	    if (clubName == null || clubName == '') {
	        layer.msg("俱乐部名称不能为空!");
	        return;
	    }
	    if (clubName.length > 20) {
            layer.msg("俱乐部名称最多输入20个字符!");
            return;
	   }
       valiMinisterName();//用户是否存在，并且是否满足条件
       checkClubNoIsExist($('#clubNo').val());
	}

	//提交时验证
	//验证填写的管理员
	//查询用户是否存在，是否满足成为部长条件
	function valiMinisterName() {
        var clubIntro = $("#clubIntro").val();
        var clubId = $('#rowId').val()
        if (clubId == null) {
            clubId = 0;
        }
        var ministerName = $('#ministerName').val();
        if (!CheckUtil.isNotNull(ministerName)) {
            layer.msg("管理员不能为空!");
            return;
        }
        var ministerNameSplit = ministerName.split(",");
        for (i=0;i<ministerNameSplit.length ;i++ ) {
            if(!CheckUtil.isMobile(ministerNameSplit[i])) {
                layer.msg('第'+(i+1)+'个帐号格式不正确!');
                return;
            }
        }
		$.post(JSSYSMANAGERV1 + "/pubgUser/checkPubgUserInfo?clubId="+clubId+"&userAccountArray="+ministerName,function(result){
			if (result != null) {
				if (result.code == '-1') {
					layer.msg("用户验证出错！");
					return;
				} else if (result.code == '-2') {
					layer.msg(result.message);
					return;
				} else if (result.code == '0') {
                    if (clubIntro == null || clubIntro == '') {
                        layer.msg("介绍不能为空!");
                        return;
                    }
                    if (clubIntro.length > 200) {
                        layer.msg("介绍不能超过200个字!");
                        return;
                    }
                    valiInviteCode();
				}
			}
		});
	}
	
	//验证邀请码是否唯一
	function valiInviteCode() {
        var inviteCode = $("#inviteCode").val();
        var exchageRatio = $("#exchageRatio").val();
        if (inviteCode == null || inviteCode == '') {
            layer.msg("邀请码不能为空!");
            return;
        }
        if (inviteCode.length != 6) {
            layer.msg("邀请码长度只能为6个字符!");
            return;
		}
		if (!CheckUtil.isInviteCode(inviteCode)) {
            layer.msg("邀请码只能包含字母和数字!");
            return;
		}
        var rowId = $('#rowId').val()
		if (rowId == null) {
            rowId = 0;
		}
        $.post(JSSYSMANAGERV1 + "/pubgClub/checkInviteCode?rowId="+rowId+"&inviteCode="+inviteCode,function(result){
            if (result != null) {
                if (result.code == '-1') {
                    layer.msg("邀请码验证出错！");
                    return;
                } else if (result.code == '-2') {
                    layer.msg(result.message);
                    return;
                } else if (result.code == '0') {
                    if (exchageRatio == null || exchageRatio == '') {
                        layer.msg("兑换比例不能为空!");
                        return;
                    }
                    if ($("#isReCommit").val() == 0) {
                        $("#isReCommit").val(1);
                        $("#saveAndUpdateForm").submit();
                    }
                }
            }
        });
	}

    // 验证俱乐部靓号的唯一性
    function checkClubNoIsExist(clubNo){
        if(clubNo == undefined || clubNo == ""){
            return true;
        }
        var isExist = false;
        var rowId = $('#rowId').val();
        $.ajax({
            type: "post",
            url: "${sysManagerV1}/pubgClub/checkClubNoIsExist",
            data: {"rowId": rowId, "clubNo": clubNo},
            dataType: "json",
            async: false,
            success: function(data){
                if(data.code == 0){
                    isExist = true;
                } else if(data.code == 1){
                    layer.msg("该靓号已存在!");
                } else if(data.code == -2){
                    window.location.href = "${sysManagerV1}/";
                } else {
                    layer.msg(data.message);
                }
            }
        });
        return isExist;
    }
   </script>
</html>